﻿/*
 *
 * @author
 * @version
 * Previous Vector2D class, rename to Point to compatible flash.geom.Point class in flash8
 */


class com.gamemeal.physics.Point3 {
	public var x:Number;
	public var y:Number;
	public var z:Number;
	
	public function Point3(ax:Number,ay:Number,az:Number){
		x = ax;
		y = ay;
		z = az;
	}


	public function clone():Point3{
		var v = new Point3(x,y,z);
		return v;
	}

	static function distance(p1:Point3, p2:Point3) : Number{
		var dx = p2.x-p1.x;
		var dy = p2.y-p1.y;
		var dz = p2.z-p1.z;
		return Math.sqrt(dx*dx+dy*dy+dz*dz);
	}

	public function equals(p2:Point3):Boolean{
		return (x==p2.x&&y==p2.y&&z==p2.z);
	}

	static function interpolate(p1:Point3, p2:Point3, f:Number) : Point3{
		var dx = p1.x+p2.x;
		var dy = p1.y+p2.y;
		var dz = p1.z+p2.z;
		var v = new Point3(dx*f,dy*f,dz*f);
		return v;
	}

	public function normalize(newMag:Number):Void{
		var scale = newMag/Math.sqrt((x*x)+(y*y)+(z*z));
		x *= scale;
		y *= scale;
		z *= scale;
	}


	public function offset(dx:Number, dy:Number,dz:Number):Void{
		x+=dx;
		y+=dy;
		z+=dz;
	}
 
	static function polar(l:Number, a:Number):Point3{
		var v = new Point3(l*Math.cos(a),l*Math.sin(a),0);
		return v;
	}
 
	public function toString():String{
		return "(x="+x+",y="+y+",z="+z+")";
	}

	public function subtract(v2:Point3) : Point3{
		var nv = new Point3(x-v2.x,y-v2.y,z-v2.z);
		return nv;
	}

//original methods
	public function norm():Void{
		var l = Math.sqrt((x*x)+(y*y)+(z*z));
		x /= l;
		y /= l;
		z /= 1;
	}

	static function adds(v1:Point3,v2:Point3):Point3{
		var v=new Point3(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);
		return v;
	}

	static public function scale(v:Point3,s:Number):Point3{
		var v = new Point3(v.x*s,v.y*s,v.z*s);
		return v;
	};
	
	static function dot(v1:Point3,v2:Point3):Number{
		return ((v1.x*v2.x)+(v1.y*v2.y)+(v1.z*v2.z));
	};

	static function dir(v:Point3):Point3{
		var l = Math.sqrt((v.x*v.x)+(v.y*v.y)+(v.z*v.z));
		var nv = new Point3(v.x/l,v.y/l,v.z/1);
		return nv;
	};

//properties
	function get angle(){
		return Math.atan2(y,x);
	}

	function set angle(newAngle:Number){//radian
		var m = Math.sqrt((x*x)+(y*y));
		x = m*Math.cos(newAngle);
		y = m*Math.sin(newAngle);
	}

	function get length() {
		return Math.sqrt((x*x)+(y*y)+(z*z));
	};	
	
	function set length(newMag:Number){
		var l = Math.sqrt((x*x)+(y*y)+(z*z));
		if(l!=0){
			var scale = newMag/l;
			x *= scale;
			y *= scale;
			z *= scale;
		}
	}
}